package com.ruoyi.wms.controller;

import java.util.List;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsInventory;
import com.ruoyi.wms.domain.dto.InventoryIncreaseRequest;
import com.ruoyi.wms.domain.dto.InventoryIncreaseResponse;
import com.ruoyi.wms.domain.dto.InventoryDecreaseRequest;
import com.ruoyi.wms.domain.dto.InventoryDecreaseResponse;
import com.ruoyi.wms.service.IWmsInventoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 库存信息Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Api(tags = "库存信息管理")
@RestController
@RequestMapping("/wms/inventory")
public class WmsInventoryController extends BaseController
{
    @Autowired
    private IWmsInventoryService wmsInventoryService;

    /**
     * 查询库存信息列表
     */
    @ApiOperation("查询库存信息列表")
    @PreAuthorize("@ss.hasPermi('wms:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsInventory wmsInventory)
    {
        startPage();
        List<WmsInventory> list = wmsInventoryService.selectWmsInventoryList(wmsInventory);
        return getDataTable(list);
    }

    /**
     * 导出库存信息列表
     */
    @ApiOperation("导出库存信息列表")
    @PreAuthorize("@ss.hasPermi('wms:inventory:export')")
    @Log(title = "库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsInventory wmsInventory)
    {
        List<WmsInventory> list = wmsInventoryService.selectWmsInventoryList(wmsInventory);
        ExcelUtil<WmsInventory> util = new ExcelUtil<WmsInventory>(WmsInventory.class);
        util.exportExcel(response, list, "库存信息数据");
    }

    /**
     * 获取库存信息详细信息
     */
    @ApiOperation("获取库存信息详细信息")
    @ApiImplicitParam(name = "inventoryId", value = "库存ID", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('wms:inventory:query')")
    @GetMapping(value = "/{inventoryId}")
    public AjaxResult getInfo(@PathVariable("inventoryId") Long inventoryId)
    {
        return success(wmsInventoryService.selectWmsInventoryById(inventoryId));
    }

    /**
     * 新增库存信息
     */
    @ApiOperation("新增库存信息")
    @PreAuthorize("@ss.hasPermi('wms:inventory:add')")
    @Log(title = "库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsInventory wmsInventory)
    {
        return toAjax(wmsInventoryService.insertWmsInventory(wmsInventory));
    }

    /**
     * 修改库存信息
     */
    @ApiOperation("修改库存信息")
    @PreAuthorize("@ss.hasPermi('wms:inventory:edit')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsInventory wmsInventory)
    {
        return toAjax(wmsInventoryService.updateWmsInventory(wmsInventory));
    }

    /**
     * 删除库存信息
     */
    @ApiOperation("删除库存信息")
    @ApiImplicitParam(name = "inventoryIds", value = "库存ID数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('wms:inventory:remove')")
    @Log(title = "库存信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inventoryIds}")
    public AjaxResult remove(@PathVariable Long[] inventoryIds)
    {
        return toAjax(wmsInventoryService.deleteWmsInventoryByIds(inventoryIds));
    }

    /**
     * 入库操作
     */
    @ApiOperation("入库操作")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "qty", value = "入库数量", required = true, dataType = "BigDecimal", paramType = "query"),
        @ApiImplicitParam(name = "documentNo", value = "单据号", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "operator", value = "操作人", required = true, dataType = "String", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('wms:inventory:inbound')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PostMapping("/inbound")
    public AjaxResult inbound(@RequestBody WmsInventory wmsInventory, 
                             @RequestParam BigDecimal qty,
                             @RequestParam String documentNo,
                             @RequestParam String operator)
    {
        return toAjax(wmsInventoryService.inbound(wmsInventory, qty, documentNo, operator));
    }

    /**
     * 出库操作
     */
    @ApiOperation("出库操作")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "inventoryId", value = "库存ID", required = true, dataType = "Long", paramType = "path"),
        @ApiImplicitParam(name = "qty", value = "出库数量", required = true, dataType = "BigDecimal", paramType = "query"),
        @ApiImplicitParam(name = "documentNo", value = "单据号", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "operator", value = "操作人", required = true, dataType = "String", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('wms:inventory:outbound')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping("/outbound/{inventoryId}")
    public AjaxResult outbound(@PathVariable("inventoryId") Long inventoryId,
                              @RequestParam BigDecimal qty,
                              @RequestParam String documentNo,
                              @RequestParam String operator)
    {
        return toAjax(wmsInventoryService.outbound(inventoryId, qty, documentNo, operator));
    }

    /**
     * 冻结库存
     */
    @ApiOperation("冻结库存")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "inventoryId", value = "库存ID", required = true, dataType = "Long", paramType = "path"),
        @ApiImplicitParam(name = "qty", value = "冻结数量", required = true, dataType = "BigDecimal", paramType = "query"),
        @ApiImplicitParam(name = "documentNo", value = "单据号", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "operator", value = "操作人", required = true, dataType = "String", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('wms:inventory:freeze')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping("/freeze/{inventoryId}")
    public AjaxResult freezeInventory(@PathVariable("inventoryId") Long inventoryId,
                                     @RequestParam BigDecimal qty,
                                     @RequestParam String documentNo,
                                     @RequestParam String operator)
    {
        return toAjax(wmsInventoryService.freezeInventory(inventoryId, qty, documentNo, operator));
    }

    /**
     * 解冻库存
     */
    @ApiOperation("解冻库存")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "inventoryId", value = "库存ID", required = true, dataType = "Long", paramType = "path"),
        @ApiImplicitParam(name = "qty", value = "解冻数量", required = true, dataType = "BigDecimal", paramType = "query"),
        @ApiImplicitParam(name = "documentNo", value = "单据号", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "operator", value = "操作人", required = true, dataType = "String", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('wms:inventory:unfreeze')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping("/unfreeze/{inventoryId}")
    public AjaxResult unfreezeInventory(@PathVariable("inventoryId") Long inventoryId,
                                       @RequestParam BigDecimal qty,
                                       @RequestParam String documentNo,
                                       @RequestParam String operator)
    {
        return toAjax(wmsInventoryService.unfreezeInventory(inventoryId, qty, documentNo, operator));
    }

    /**
     * 增加商品库存
     */
    @ApiOperation(value = "增加商品库存", notes = "增加指定商品的库存数量，支持新建库存记录或更新现有库存，自动记录操作日志")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "request", value = "增加库存请求参数", required = true, dataType = "InventoryIncreaseRequest", paramType = "body")
    })
    @PreAuthorize("@ss.hasPermi('wms:inventory:increase')")
    @Log(title = "增加商品库存", businessType = BusinessType.UPDATE)
    @PostMapping("/increase")
    public AjaxResult increaseInventory(@ApiParam(value = "增加库存请求参数", required = true) @RequestBody @Valid InventoryIncreaseRequest request)
    {
        InventoryIncreaseResponse response = wmsInventoryService.increaseInventory(request);
        return success(response);
    }

    /**
     * 减少商品库存
     */
    @ApiOperation(value = "减少商品库存", notes = "减少指定商品的库存数量，自动记录操作日志")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "request", value = "减少库存请求参数", required = true, dataType = "InventoryDecreaseRequest", paramType = "body")
    })
    @PreAuthorize("@ss.hasPermi('wms:inventory:decrease')")
    @Log(title = "减少商品库存", businessType = BusinessType.UPDATE)
    @PostMapping("/decrease")
    public AjaxResult decreaseInventory(@ApiParam(value = "减少库存请求参数", required = true) @RequestBody @Valid InventoryDecreaseRequest request)
    {
        InventoryDecreaseResponse response = wmsInventoryService.decreaseInventory(request);
        return success(response);
    }
}